using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Phoenix.Framework;

namespace Phoenix.Filters
{
    public class FilterProviderCollection : Collection<IFilterProvider>
    {
        static readonly FilterComparer filterComparer = new FilterComparer();
        readonly IEnumerable<IFilterProvider> internalFilters;

        public FilterProviderCollection()
        {
            internalFilters = GetInternalFilters();
        }

        IEnumerable<IFilterProvider> GetInternalFilters()
        {
            yield return new InternalFilterProvider();
            yield return new FilterAttributeFilterProvider(true);
        }

        public FilterProviderCollection(params IFilterProvider[] providers)
            : base(providers)
        {
            internalFilters = GetInternalFilters();
        }

        static bool AllowMultiple(object filterInstance)
        {
            var mvcFilter = filterInstance as IMvcFilter;
            return mvcFilter == null || mvcFilter.AllowMultiple;
        }

        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, IActionDescriptor actionDescriptor)
        {
            IEnumerable<Filter> combinedFilters = this
                .Concat(internalFilters)
                .SelectMany(fp => fp.GetFilters(controllerContext, actionDescriptor))
                .OrderBy(filter => filter, filterComparer);

            // Remove duplicates from the back forward
            return RemoveDuplicates(combinedFilters.Reverse()).Reverse();
        }

        static IEnumerable<Filter> RemoveDuplicates(IEnumerable<Filter> filters)
        {
            var visitedTypes = new HashSet<Type>();

            foreach (var filter in filters)
            {
                var filterInstance = filter.Instance;
                var filterInstanceType = filterInstance.GetType();

                if (!visitedTypes.Contains(filterInstanceType) || AllowMultiple(filterInstance))
                {
                    yield return filter;
                    visitedTypes.Add(filterInstanceType);
                }
            }
        }

        class FilterComparer : IComparer<Filter>
        {
            public int Compare(Filter x, Filter y)
            {
                // Nulls always have to be less than non-nulls
                if (x == null && y == null)
                {
                    return 0;
                }
                if (x == null)
                {
                    return -1;
                }
                if (y == null)
                {
                    return 1;
                }

                // Sort first by order...

                if (x.Order < y.Order)
                {
                    return -1;
                }
                if (x.Order > y.Order)
                {
                    return 1;
                }

                // ...then by scope

                if (x.Scope < y.Scope)
                {
                    return -1;
                }
                if (x.Scope > y.Scope)
                {
                    return 1;
                }

                return 0;
            }
        }
    }
}